package com.zqh.productcommentquery.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zqh.myfmmall.clients.UserQueryClient;
import com.zqh.myfmmall.entity.ProductComments;
import com.zqh.myfmmall.entity.Users;
import com.zqh.myfmmall.result.Result;
import com.zqh.myfmmall.vo.ProductCommentsVO;
import com.zqh.productcommentquery.dao.ProductCommentDao;
import com.zqh.productcommentquery.service.ProductCommentQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description :
 * @Author :kono淇杭哒、
 * @Create 2022-02-18 2:19
 */
@Service("productCommentQueryService")
public class ProductCommentQueryServiceImpl extends ServiceImpl<ProductCommentDao, ProductComments> implements ProductCommentQueryService{
    @Autowired
    private UserQueryClient userQueryClient;


    @Override
    public Result page(Integer pageNum, Integer pageSize, String productId) {
        HashMap<String, Object> pageMap = new HashMap<>();
        Page<ProductComments> page = new Page<>(pageNum,pageSize);
        query().eq("product_id", productId).page(page);
        pageMap.put("total",page.getTotal());
        pageMap.put("records",transToVo(page.getRecords()));
        return Result.success(pageMap);
    }

    @Override
    public Result retrieveCommentsCount(String productId) {
        long total = 0;
        QueryWrapper<ProductComments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",productId).groupBy("comm_level").select("comm_level,count(*) as count");
        List<Map<String, Object>> queryResultMapList = listMaps(queryWrapper);
        HashMap<String, Object> resultMap = new HashMap<>();
        for (Map<String, Object> queryResultMap : queryResultMapList) {
            long currentLevelCount =  (long) queryResultMap.get("count");
            if (queryResultMap.get("comm_level").equals(1)){
                resultMap.put("好评",currentLevelCount);
                total += currentLevelCount;
            }else if(queryResultMap.get("comm_level").equals(2)){
                resultMap.put("中评",currentLevelCount);
                total += currentLevelCount;
            }else if(queryResultMap.get("comm_level").equals(3)){
                resultMap.put("差评",currentLevelCount);
                total += currentLevelCount;
            }
        }
        resultMap.put("总数",total);
        return Result.success(resultMap);
    }


    public ArrayList<ProductCommentsVO> transToVo(List<ProductComments> productComments){
        ArrayList<ProductCommentsVO> productCommentsVOS = new ArrayList<>();
        for (ProductComments productComment : productComments) {
            String userId = productComment.getUserId();
            Result result = userQueryClient.retrieveUserBasicInfo(userId);
            Users user = JSON.parseObject(JSON.toJSONString(result.getData()), Users.class);
            productCommentsVOS.add(new ProductCommentsVO(productComment,user));
        }
        return productCommentsVOS;
    }
}
